/*
 * Copyright 2007 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

package com.cnc.bas;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.Order;
import org.springframework.dao.DataAccessException;
import com.cnc.bas.common.HQueryInfo;
import com.cnc.bas.common.SQueryInfo;
import com.cnc.bas.dao.IBasDao;
import com.cnc.bas.dao.IBasSqlDao;
import com.cnc.bas.seq.ISeqService;
import com.cnc.bas.util.StringUtil;
import com.cnc.bas.web.SecureBaseDispatchAction;

/**
 * @author Du bf
 * @version 1.0  2005-3-25
 */
public class BasServiceImpl implements IBasService {
	private static final int intMaxResults = Integer.parseInt(ConfigManager.getInstance().getConfigValue("MaxResults", "2000"));

	public static Log logger = LogFactory.getLog(BasServiceImpl.class);	
    private IBasDao basDao;
    private IBasSqlDao basSqlDao;
    private ISeqService seqService;

    public IBasDao getBasDao() {
        return basDao;
    }

    public void setBasDao(IBasDao basDao) {
        this.basDao = basDao;
    }

    public IBasSqlDao getBasSqlDao() {
        return basSqlDao;
    }

    public void setBasSqlDao(IBasSqlDao basSqlDao) {
        this.basSqlDao = basSqlDao;
    }

    public ISeqService getSeqService() {
        return seqService;
    }

    public void setSeqService(ISeqService seqService) {
        this.seqService = seqService;
    }

    public void add(Object vo) {
        basDao.add(vo);
    }

    public void update(Object vo) {
        basDao.update(vo);
    }

    public void delete(Class entityClass, Serializable id) {
        basDao.delete(entityClass, id);
    }

    public Object load(Class entityClass, Serializable id) {
        return basDao.load(entityClass, id);
    }

    public List list(Object param, String order) {
        if (order == null) return basDao.findByObjectParam(param, null);
        String[] arrOrder = order.split(",");
        Order[] ord = new Order[arrOrder.length];
        for (int i = 0; i < arrOrder.length; i++) {
            ord[i] = Order.asc(arrOrder[i]);
        }
        return basDao.findByObjectParam(param, ord);
    }

    public List listAll(Class entityClass, String order) {
        return basDao.listAll(entityClass, order);
    }
    
    public HQueryInfo query(final HQueryInfo hqlWrapper) {
    	return this.query(hqlWrapper, false);
    }
    /**
     * 根据HqlWrapper(Hibernate查询条件及分页管理包装类)查询符合条件的对象，把查询结果list及count放置到该HqlWrapper并返回
     *
     * @param queryInfo Hibernate查询条件及分页管理包装类
     * @return HqlWrapper 包含查询结果list及count的hqlWrapper
     * @see com.longtop.intelliwebframework.common.HqlWrapper
     */
    public HQueryInfo query(final HQueryInfo queryInfo, final boolean isCache) {
        if (queryInfo == null) return null;

        //格式化参数值
        
        String className  = queryInfo.getQueryClass().getName();
        String hql = " from "+className;
        String[] propertyNames = StringUtil.listToStrArray(queryInfo.getPropertyNames());
        String[] operators = StringUtil.listToStrArray(queryInfo.getOperators());

        List valuesList = queryInfo.getValues();
        Object[] values = valuesList == null || valuesList.size() == 0 ? null : valuesList.toArray();

        int offset = queryInfo.getOffset();
        int size = queryInfo.getPageSize();
        boolean isTotalSize = queryInfo.isCountTotalSize();
        String orderBy = queryInfo.getOrderBy();
        String groupBy = queryInfo.getGroupBy();
        String otherCause = queryInfo.getOtherHql();
        boolean isShowAll = queryInfo.isShowAll();
        if(isShowAll)
        	size = intMaxResults;
        //执行查询
        Object[] result = basDao.query(hql, propertyNames, operators, values, offset, size, isTotalSize, orderBy, groupBy, otherCause);

        //返回查询结果并把结果设置给QueryInfo
        queryInfo.setQueryList((List) result[0]);
        queryInfo.setRecordCount(result[1] != null ? ((Integer) result[1]).intValue() : 0);

        //返回QueryInfo
        return queryInfo;
    }
    
    /**
     * 根据SQueryInfo(IBatis查询条件及分页管理包装类)查询符合条件的对象，把查询结果list及count放置到该QueryInfo并返回
     *
     * @param queryInfo IBatis查询条件及分页管理包装类
     * @return queryInfo 包含查询结果list及count的SQueryInfo
     */
    public SQueryInfo query(SQueryInfo queryInfo) throws DataAccessException {
        String statementId = queryInfo.getStatementId();
        Object paramValue = queryInfo.getParamValue();
        int offset = queryInfo.getOffset();
        int size = queryInfo.getPageSize();
        if(queryInfo.isShowAll())
        	size = intMaxResults;

        //取得查询结果列表
        
        Object[] result = basSqlDao.queryPageForListBySql(statementId, paramValue, offset, size);
        queryInfo.setQueryList((List) result[0]);
        queryInfo.setRecordCount(result[1] != null ? ((Integer) result[1]).intValue() : 0);
        //返回QueryInfo
        return queryInfo;
    }
     

    /**
     * 查询符合条件的记录，生成Object返回。
     *
     * @see 
     */
    public Object queryForObjectBySql(String statementId, Object value) {
        return basSqlDao.queryForObjectBySql(statementId, value);
    }
       
}
